package com.heima.wemedia.aop;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.threadlocal.WmThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


/**
 * 验证是否登录的切面
 */
@Aspect
@Component
@Slf4j
public class AuthenticationAspect {

    //定义一个切点,表示注解加在哪里，哪里就是切点
    @Pointcut("@annotation(com.heima.wemedia.annotation.Authentication)")
    public void aspect() {
    }

    /**
     * 环绕通知
     */
    @Around("aspect()")
    public Object isLogIn(ProceedingJoinPoint joinPoint) throws Throwable {
        WmUser user = WmThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        return joinPoint.proceed();
    }
}
